﻿using System;
using System.Collections.Generic;
using Kratos.Framework.ContentManagement.Models.SystemRights;
using Kratos.Framework.Extensions;
using Kratos.Framework.Infrastructures;
using Kratos.Framework.Repositories;

namespace Kratos.Framework.Modules.Core.Services.Impl
{
    public class GroupService : IGroupService
    {
        private readonly IRepository<Group> _groupRep;

        public GroupService(IRepository<Group> groupRep)
        {
            _groupRep = groupRep;
        }

        public PageList<Group> GetListByPage(int? pageIndex, int? pageSize, out int totalCount)
        {
            pageIndex = pageIndex ?? 0;
            pageSize = pageSize ?? 16;
            var pageList = _groupRep.GetAll().ToPageList(pageIndex.Value, pageSize.Value);
            totalCount = pageList.RecordCount;
            return pageList;
        }

        public void Save(Group model)
        {
            model.CreateTime = model.CreateTime ?? DateTime.Now;
            _groupRep.Save(model);
        }

        public void Delete(Group model)
        {
            _groupRep.DeleteOnSubmit(model);
        }

        public void Update(Group model)
        {
            var originalModel = Get(model.Id.Value);
            originalModel.GroupName = model.GroupName;
            originalModel.Description = model.Description;
            originalModel.State = model.State;

            _groupRep.SaveOrUpdate(originalModel);
        }

        public Group Get(int id)
        {
            return _groupRep.GetById(id);
        }
    }
}
